import {Component, OnDestroy, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute} from "@angular/router";
import {Subscription} from "rxjs/Subscription";
import {Router} from "@angular/router";
import {Content} from "@shared/components/toolbar/toolbar-content";
import {ToolsService} from "@shared/service/tools/tools.service";
import {ToolbarService} from "@shared/components/toolbar/toolbar.service";
import {SponsorService} from "@sponsor/sponsor.service";

@Component({
  selector: 'seed-sponsor-detail',
  templateUrl: './sponsor-detail.component.html',
  styleUrls: ['./sponsor-detail.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class SponsorDetailComponent implements OnInit, OnDestroy {
  public hasContact = false;
  public sponsorId: any;
  public toggleContact = false;
  subscription: Subscription = new Subscription();
  public sponsorDetail: any;
  @ViewChild(Content) content;

  constructor(public route: ActivatedRoute,
              public toolsService: ToolsService,
              public toolBarService: ToolbarService,
              public sponsorService: SponsorService,
              public router: Router) {
  }

  ngOnInit() {
    this.toolsService.showLoading();
    this.subscription.add(
      this.route.queryParams.do(() => this.toolBarService.unfoldNav()).subscribe(param => {
        this.sponsorId = param['planId'];
        this.getSponsorDetail(param['planId']);
      })
    );
  }

  getSponsorDetail(planId) {
    this.sponsorService.getSponsorDetail(planId).subscribe(res => {
      if (res.result === '0') {
        this.sponsorDetail = res.data;
        this.toolsService.hideLoading();
      }
    });
  }

  Contact() {
    this.toggleContact = true;
    this.content.scrollTop();
    this.content.disableScroll();
    this.hasContact = true;
  }

  closeContact() {
    this.toggleContact = false;
    this.hasContact = false;
    this.content.enableScroll(true);
  }

  gotoHomepage(userId) {
    this.router.navigate([`/`, userId]);
  }

  submit(contactInfo) {
    this.toggleContact = false;
    this.subscription.add(
      this.sponsorService.addContactInfo(contactInfo.contactObj).subscribe(res => {
        if (res.result === '0') {
          this.toolsService.showToast('提交成功');
        }
      })
    );
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}
